package it.univaq.mwt.facade;

import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import it.univaq.mwt.business.GiftService;
import it.univaq.mwt.exceptionmonitor.LivelloEx;
import it.univaq.mwt.exceptionmonitor.WishListrException;
import it.univaq.mwt.exceptionmonitor.WishlistUserException;
import it.univaq.mwt.exceptionmonitor.WishlistrMessage;
import it.univaq.mwt.log.LivelloLog;
import it.univaq.mwt.log.LogManager;
import it.univaq.mwt.model.bean.GiftBeanRemote;
import it.univaq.mwt.model.entity.Files;
import it.univaq.mwt.model.entity.Gift;

public class FacadeGift implements GiftService,LivelloEx, LivelloLog {



private GiftBeanRemote gbr;
	

	public FacadeGift(GiftBeanRemote gbr) {
		super();
		this.gbr = gbr;
	}
	
	
	public void createGift(String title, String link, String description,int id_list) throws WishlistUserException {
		
		Gift gift=new Gift();
		gift.setLink_name(title);
		gift.setLink(link);
		gift.setDescription(description);
		
		try {
			try {
				gbr.registerGift(gift,id_list);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - createGift :" + e.getMessage(), ERR);
				// throws
			}
		
			LogManager.getIstance().scrivi("FacadeGift - createGift :" + gift.getName() , RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}		
	}
	
	
	public Set<Gift> getGiftList(int id_list){
	
		Set<Gift> giftlist=new HashSet<Gift>();
		
		try {
			try {
				giftlist=gbr.getGiftList(id_list);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getGiftList :" + e.getMessage(), ERR);
				// throws
			}

			LogManager.getIstance().scrivi("FacadeGift - getGiftList : Richiesta lista delle Gift", RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		return giftlist;
	}

	
	public Gift createGift(Gift gift) throws WishlistUserException {

		Gift g = null;
		try {
			try {
				g = gbr.createGift(gift);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - createGift :" + e.getMessage(), ERR);
				// throws
			}
				
			LogManager.getIstance().scrivi("FacadeGift - createGift : E' stata creata la gift " + g.getName(), RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		} 
		return g;
	}

	
	public Gift editGift(Gift gift) throws WishlistUserException {

		Gift g = null;
		try {
			try {
				g = gbr.editGift(gift);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - editGift :" + e.getMessage(), ERR);
				// throws
			}
		
			LogManager.getIstance().scrivi("FacadeGift - editGift : Modificata la gift " + g.getName(), RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		
		return g;
	}

	
	public void deleteGift(int id_gift) throws WishlistUserException {
		try {
			try {
				gbr.deleteGift(id_gift);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - deleteGift :" + e.getMessage(), ERR);
				// throws
			}
		
			LogManager.getIstance().scrivi("FacadeGift - deleteGift : Cancellazione della gift con id = " + id_gift, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
	}

	
	public Gift getGift(int id) {

		Gift gift = null;
		
		try {
			try {
				gift = gbr.getGift(id);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getGift :" + e.getMessage(), ERR);
				// throws
			}
		
			LogManager.getIstance().scrivi("FacadeGift - getGift : Richiesta gift con id = " + id, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		return gift;
	}

	
	public int getGiftNumber() {
		
		int number = 0;

		try {
			try {
				number = gbr.getGiftNumber();
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getGiftListByUserId :" + e.getMessage(), ERR);
				// throws
			}
			LogManager.getIstance().scrivi("FacadeGift - getGiftNumber : Numero totale gift pari a " + number, RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		return number;
	}

	
	public List<Gift> getList(int i, int j){

		List<Gift> list = null;
		
		
		try {
			try {
				list = gbr.getGiftList(i,j);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getGift :" + e.getMessage(), ERR);
				// throws
			}
			
			LogManager.getIstance().scrivi("FacadeGift - getList : Richiesta lista Gift", RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		
		return list;
	}

	
	public void addFile(Gift gift, Files file) throws WishlistUserException {

		try {
			try {
				gbr.addFile(gift,file);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - addFile :" + e.getMessage(), ERR);
				// throws
			}
			LogManager.getIstance().scrivi("FacadeGift - addFile : Richiesta aggiunta File " + file.getName(), RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
	}

	
	public void editFile(Gift gift, Files file) throws WishlistUserException {

		try {
			try {
				gbr.editFile(gift,file);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - editFile :" + e.getMessage(), ERR);
				// throws
			}
			
			LogManager.getIstance().scrivi("FacadeGift - editFile : Richiesta modifica File " + file.getName(), RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
	}

	
	public List<Gift> getListSmall(int limit){
		
		List<Gift> list =null;

		try {
			try {
				list = gbr.getGiftListSmall(limit);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getListSmall :" + e.getMessage(), ERR);
				// throws
			}
			
			LogManager.getIstance().scrivi("FacadeGift - getListSmall : Richiesta la lista " , RUN);
		} catch (IOException e) {
			new WishListrException(e,ERROR);
		}
		
		return list;
	}

	
	public List<Gift> getGiftListByUserId(int id_user) {
		
		List<Gift> list = null;
		try {
			try {
				list = gbr.getGiftListByUserId(id_user);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getGiftListByUserId :" + e.getMessage(), ERR);
				// throws
			}
			
			LogManager.getIstance().scrivi("FacadeGift - getGiftListByUserId :", RUN);	
		} catch (IOException e1) {
			new WishListrException(e1,ERROR);
		}
		
		return list;
	}


	@Override
	public List<Gift> getGiftListByCategoryId(int id_category) {
		List<Gift> list = null;
		try {
			try {
				list = gbr.getGiftListByCategoryId(id_category);
			} catch (WishlistrMessage e) {
				LogManager.getIstance().scrivi("FacadeGift - getGiftListByCategoryId :" + e.getMessage(), ERR);
				// throws
			}
			
			LogManager.getIstance().scrivi("FacadeGift - getGiftListByCategoryId :", RUN);	
		} catch (IOException e1) {
			new WishListrException(e1,ERROR);
		}
		
		return list;
	}
	
	
	
}